﻿using UnityEngine;
using TinyWnd;

public class MatchResultContent : WndContentData
{
    public bool isWin;
}

public class MatchResultWnd : Wnd
{
    UILabel label;
    GameObject goBtnGoLevel;
    GameObject goBtnGoSkill;
    GameObject goBtnContinue;
    UIGrid grid;
    TweenScale twScale;

    protected override void OnAwake()
    {
        _wndId = WndId.matchResult;

        var tran = transform;
        label = Utility.FindChild<UILabel>(tran, "Bg/Content");
        goBtnGoLevel = Utility.FindChild(tran, "Bg/Grid/01BtnGoLevel").gameObject;
        goBtnGoSkill = Utility.FindChild(tran, "Bg/Grid/02BtnGoSkill").gameObject;
        goBtnContinue = Utility.FindChild(tran, "Bg/Grid/03BtnContinue").gameObject;
        twScale = Utility.FindChild<TweenScale>(tran, "Bg");
        grid = Utility.FindChild<UIGrid>(tran, "Bg/Grid");

        UIEventListener.Get(goBtnGoLevel).onClick = delegate
        {
            Close(false);
            BattleMgr.inst.LeaveBattle();
            WndMgr.inst.ShowWindow(WndId.level);
        };

        UIEventListener.Get(goBtnGoSkill).onClick = delegate
        {
            Close(false);
            BattleMgr.inst.LeaveBattle();
            WndMgr.inst.ShowWindow(WndId.skill);
        };

        UIEventListener.Get(goBtnContinue).onClick = delegate
        {
            Close(false);
            BattleMgr.inst.LeaveBattle();
        };
    }

    protected override void OnShow(WndContentData content)
    {
        var data = content as MatchResultContent;
        label.text = data.isWin ? "You win BB" : "You lose BB";

        if (data.isWin)
        {
            goBtnGoLevel.SetActive(false);
            goBtnGoSkill.SetActive(false);
        }
        else
        {
            goBtnGoLevel.SetActive(true);
            goBtnGoSkill.SetActive(true);
        }
        grid.repositionNow = true;

        gameObject.SetActive(true);
        twScale.onFinished.Clear();
        twScale.PlayForward();
    }

    protected override void OnHide()
    {
        twScale.SetOnFinished(CbFinishClose);
        twScale.PlayReverse();
    }

    void CbFinishClose()
    {
        gameObject.SetActive(false);
    }
}
